{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 74
    },
    "colab_type": "code",
    "id": "l40ba7zfNCbe",
    "outputId": "0eebb93f-08d2-47e0-8292-2184ff3e4b50"
   },
   "source": [
    "# Start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T01:01:07.415880Z",
     "start_time": "2019-05-28T01:01:07.011218Z"
    },
    "colab": {},
    "colab_type": "code",
    "id": "2man52H0NLYV"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "\n",
    "import torch\n",
    "\n",
    "DIR_MAIN = os.path.abspath('../../../repo1/') #'../../../CS4180-DL/'\n",
    "sys.path.append(DIR_MAIN) \n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T01:01:39.140855Z",
     "start_time": "2019-05-28T01:01:07.417358Z"
    },
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 993
    },
    "colab_type": "code",
    "id": "72h-COiKN1s-",
    "outputId": "16600db5-9cb1-4f38-adf3-6f23068a4040",
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  0%|          | 0/16551 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " -- init_epoch :  0\n",
      " -- max_epochs :  5\n",
      " ---------------------------- EPOCH :  0  ---------------------------------- \n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 1/16551 [00:00<1:15:24,  3.66it/s]/home/strider/anaconda3/lib/python3.5/site-packages/torch/nn/functional.py:1332: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.\n",
      "  warnings.warn(\"nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.\")\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " - data (or X) :  torch.Size([1, 3, 416, 416]) torch.float32\n",
      " - target (or Y) :  torch.Size([1, 250]) torch.float64\n",
      " - Total train points :  16551  || nsamples :  16551\n",
      " i : 0 / 16551\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/strider/anaconda3/lib/python3.5/site-packages/torch/nn/_reduction.py:49: UserWarning: size_average and reduce args will be deprecated, please use reduction='sum' instead.\n",
      "  warnings.warn(warning.format(ret))\n",
      "  0%|          | 28/16551 [00:27<5:20:23,  1.16s/it]\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/queues.py\", line 240, in _feed\n",
      "    send_bytes(obj)\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 200, in send_bytes\n",
      "    self._send_bytes(m[offset:offset + size])\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 404, in _send_bytes\n",
      "    self._send(header + buf)\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 368, in _send\n",
      "    n = write(self._handle, buf)\n",
      "BrokenPipeError: [Errno 32] Broken pipe\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/queues.py\", line 240, in _feed\n",
      "    send_bytes(obj)\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 200, in send_bytes\n",
      "    self._send_bytes(m[offset:offset + size])\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 404, in _send_bytes\n",
      "    self._send(header + buf)\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 368, in _send\n",
      "    n = write(self._handle, buf)\n",
      "BrokenPipeError: [Errno 32] Broken pipe\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/queues.py\", line 240, in _feed\n",
      "    send_bytes(obj)\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 200, in send_bytes\n",
      "    self._send_bytes(m[offset:offset + size])\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 404, in _send_bytes\n",
      "    self._send(header + buf)\n",
      "  File \"/home/strider/anaconda3/lib/python3.5/multiprocessing/connection.py\", line 368, in _send\n",
      "    n = write(self._handle, buf)\n",
      "BrokenPipeError: [Errno 32] Broken pipe\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-54aa641543e2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[0mtrainObj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mYOLOv2Train\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0mtrainObj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPASCAL_TRAIN\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mPASCAL_VALID\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTRAIN_LOGDIR\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mMODEL_CFG\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mMODEL_WEIGHT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/home/strider/Work/Netherlands/TUDelft/1_Courses/Sem2/DeepLearning/Project/repo1/src/train.py\u001b[0m in \u001b[0;36mtrain\u001b[0;34m(self, PASCAL_TRAIN, PASCAL_VALID, TRAIN_LOGDIR, MODEL_CFG, MODEL_WEIGHT)\u001b[0m\n\u001b[1;32m    161\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    162\u001b[0m                     \u001b[0;32mif\u001b[0m \u001b[0muse_cuda\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 163\u001b[0;31m                         \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcuda\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    164\u001b[0m                         \u001b[0;31m#target= target.cuda()\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    165\u001b[0m                     \u001b[0mt3\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "torch.cuda.empty_cache()\n",
    "from src.train import YOLOv2Train\n",
    "from tensorboardcolab import TensorBoardColab\n",
    "\n",
    "if (1):\n",
    "    DIR_MAIN         = 'CS4180-DL'\n",
    "    print (' - 1. DIR_MAIN :  ', DIR_MAIN)\n",
    "    \n",
    "if (1):\n",
    "    PASCAL_DIR   = os.path.join(DIR_MAIN, 'data/dataset/VOCdevkit/')\n",
    "    PASCAL_TRAIN = os.path.join(DIR_MAIN, 'data/dataset/VOCdevkit/voc_train.txt')\n",
    "    PASCAL_VALID = os.path.join(DIR_MAIN, 'data/dataset/VOCdevkit/2007_test.txt')\n",
    "    TRAIN_LOGDIR = os.path.join(DIR_MAIN, 'train_data')\n",
    "    VAL_LOGDIR   = os.path.join(DIR_MAIN, 'eval_data')\n",
    "    VAL_OUTPUTDIR_PKL = os.path.join(DIR_MAIN, 'eval_results')\n",
    "    MODEL_CFG    = os.path.join(DIR_MAIN, 'data/cfg/github_pjreddie/yolov2-voc.cfg')\n",
    "    # MODEL_WEIGHT = os.path.join(DIR_MAIN, 'data/weights/pruned/yolov2-voc-weight-prune-30.0.weights')\n",
    "    MODEL_WEIGHT = os.path.join(DIR_MAIN, 'data/dataset/weights/yolov2-voc.weights')\n",
    "    print (' - 2. MODEL_WEIGHT :  ', MODEL_WEIGHT)\n",
    "    \n",
    "if (1):\n",
    "    VAL_PREFIX   = 'pretrained'\n",
    "    BATCH_SIZE   = 1;\n",
    "    print (' - 3. VAL_PREFIX : ', VAL_PREFIX)\n",
    "\n",
    "if (1):\n",
    "    try:\n",
    "        print (' - 4. Logger : ', LOGGER)\n",
    "    except:\n",
    "        LOGGER = TensorBoardColab()\n",
    "        print (' - 4. Logger : ', LOGGER)\n",
    "\n",
    "\n",
    "if (1):\n",
    "    trainObj = YOLOv2Train()\n",
    "    trainObj.train(PASCAL_DIR, PASCAL_TRAIN, PASCAL_VALID, TRAIN_LOGDIR, VAL_LOGDIR, VAL_OUTPUTDIR_PKL, VAL_PREFIX\n",
    "                   , MODEL_CFG, MODEL_WEIGHT\n",
    "                   , BATCH_SIZE\n",
    "                   , LOGGER)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T01:01:39.160432Z",
     "start_time": "2019-05-28T01:01:39.142072Z"
    }
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'torch' has no attribute 'Variable'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-e1862b403929>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mVariable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: module 'torch' has no attribute 'Variable'"
     ]
    }
   ],
   "source": [
    "torch.Variable"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "anaconda-cloud": {},
  "colab": {
   "collapsed_sections": [],
   "name": "Yolov2.ipynb",
   "provenance": [],
   "version": "0.3.2"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
